package com.fy.controller;

import com.fy.dao.UserMapper;
import com.fy.dao.User_infoMapper;
import com.fy.entity.U_co;
import com.fy.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    String id = "5";
    @Resource
    UserMapper userMapper;

    @Resource
    User_infoMapper userInfoMapper;

    //得到数据库用户表
    @GetMapping("/getAll")
    public List<User> getAll() {
        return userMapper.findAll();
    }

    //注册接口
    @PostMapping("/insert")

    public String insert(String user_id, String user_account, String user_pwd, String user_tel, String user_avatar, String user_state) {
        List<User> all = userMapper.findAll();
        for (User user : all) {
            if (user.getUser_id() == null) {
                user_id = id;
            }
            if (user.getUser_account().equals(user_account)) {
                return "401";//用户账号已存在
            }
            if (user.getUser_tel().equals(user_tel)) {
                return "402";//用户手机号已被注册
            }
        }
        userMapper.insert(id, user_account, user_pwd, user_tel, user_state);
        userInfoMapper.insert_info(id, id, null, null, null, null, null);
        id = (Integer.valueOf(id) + 1 + "");
        return "200";//创建成功
    }

    @PostMapping("/getInfo")
    public List<User> getInfo(String user_id) {
        return userMapper.getInfo(user_id);
    }

    //登陆接口
    @PostMapping("/query")
    public String query(String user_account, String user_pwd) {
        List<User> all = userMapper.findAll();
        for (User user : all) {
            if (user.getUser_account().equals(user_account)) {
                if (user.getUser_pwd().equals(user_pwd)) {
                    userMapper.query(user_account, user_pwd);
                    return "200";//登陆成功
                } else {
                    return "403";//密码错误
                }
            } else {
                return "404";//没有账号
            }

        }
        return "0";
    }

    //修改密码
    @PostMapping("/revise")
    public int revise(String user_pwd, String user_id) {
        List<User> all = userMapper.findAll();
        for (User user : all) {
            if (user.getUser_pwd().equals(user_pwd)) {
                return 405;//新旧密码重复
            }
        }
        userMapper.revise(user_pwd, user_id);
        return 200;//修改密码成功
    }

}
